package com.source.tiershop.client;

// wsdl generieren
// "C:\Program Files\Java\jdk1.6.0_26\bin\wsimport" -Xnocompile -s src xxx/svc/Logon.wsdl


import java.util.ArrayList;
import java.util.List;

import webservices.framework.affilinet.types.productservice.Shop;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;


import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.source.tiershop.client.GreetingService;
import com.source.tiershop.client.GreetingServiceAsync;
import com.source.tiershop.shared.CShop;
import com.source.tiershop.shared.FieldVerifier;
//import com.source.tiershop.client.ProductService;
//import com.source.tiershop.client.ProductServiceAsync;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TierShop implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
//	private final ProductServiceAsync productService = GWT.create(ProductService.class);
	
	private TextCell textCell = new TextCell();
	private CellList<String> cellList = new CellList<String>(textCell);
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		RootPanel.get().add(getViewPanel());
		
		 // Create a CellList that uses the cell.
		    cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		 // Add a selection model to handle user selection.
		    final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
		    cellList.setSelectionModel(selectionModel);
		    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
		      public void onSelectionChange(SelectionChangeEvent event) {
		        String selected = selectionModel.getSelectedObject();
		        if (selected != null) {
		          Window.alert("You selected: " + selected);
		        }
		      }
		    });
		
	cellList.setTitle("List aller Shops");
	
		RootPanel.get().add(cellList);    
	}
	

    
   
	public ZipForm getViewPanel() {
	    final ZipForm form = new ZipForm();
	    form.setLeft(50);
	    form.setTop(50);
	    form.setNumCols(3);
	    form.setCellSpacing(5);

	    TextItem zipField = new TextItem();
	    zipField.setName("ZipCode");
	    zipField.addKeyPressHandler(new KeyPressHandler() {
	      public void onKeyPress(KeyPressEvent event) {
	        if (event.getKeyName().equals("Enter")) {
	          form.callService();
	        }
	      }
	    });

	    ButtonItem findButton = new ButtonItem("findCity", "Find City");
	    findButton.setStartRow(false);
	    findButton.addClickHandler(new ClickHandler() {
	      public void onClick(ClickEvent event) {
	        form.callService();
	      }
	    });

	    StaticTextItem cityText = new StaticTextItem();
	    cityText.setName("City");


	    
	    form.setItems(zipField, findButton, cityText);

	    return form;

	  }
	
	  class ZipForm extends DynamicForm {
		  	private void callService() {
				// First, we validate the input.
				String textToServer = getValueAsString("ZipCode");
				if (!FieldVerifier.isValidName(textToServer)) {
					return;
				}

//				// Then, we send the input to the server.
//				greetingService.greetServer(textToServer,
//						new AsyncCallback<String>() {
//							public void onFailure(Throwable caught) {
//								// Show the RPC error message to the user
//
//								ZipForm.this.setValue("City", "error" + caught.getMessage());
//							}
//
//							public void onSuccess(String result) {
//								ZipForm.this.setValue("City", (String) result);
//							}
//						});
				
//				greetingService.greetServer2(textToServer,
//						new AsyncCallback<List<String>>() {
//							public void onFailure(Throwable caught) {
//								// Show the RPC error message to the user
//		
//								ZipForm.this.setValue("City", "error" + caught.getMessage());
//							}
//	
//							@Override
//							public void onSuccess(List<String> shopList) {
//								// TODO Auto-generated method stub
//								// Set the total row count. This isn't strictly necessary, but it affects
//								// paging calculations, so its good habit to keep the row count up to date.
//								cellList.setRowCount(shopList.size(), true);
//
//								// Push the data into the widget.
//								cellList.setRowData(0, shopList);								
//							}
//						});
				
				
				
				greetingService.greetServer3(textToServer,
						new AsyncCallback<List<CShop>>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
		
								ZipForm.this.setValue("City", "error" + caught.getMessage());
							}
	
							@Override
							public void onSuccess(List<CShop> shopList) {
								// TODO Auto-generated method stub
								// Set the total row count. This isn't strictly necessary, but it affects
								// paging calculations, so its good habit to keep the row count up to date.

								List<String> listOfShops = new ArrayList<String>();
 				
					 			String shopName = null;
					
					 			
					 		    for(int i = 0, size = shopList.size(); i<size; i++){
					 		    	shopName = shopList.get(i).getTitle();
					 				listOfShops.add(shopName);
							     }

								cellList.setRowCount(shopList.size(), true);

								// Push the data into the widget.
								cellList.setRowData(0, listOfShops);								
							}
						});				
				
				
				
				
		  	}    
	}
}
